using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class TrashcanBItem : BaseItem
	{
		public enum State
		{
			Close = 0,
			Open = 1
		}

		private State state;

		private Transform destoryPoint;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			destoryPoint = model.Find("DestroyPoint");
			SetState((State)data.state);
		}

		private void SetState(State val)
		{
			if (state != val)
			{
				state = val;
				model.Find("Open").SetObjectActive(state == State.Open);
				model.Find("Close").SetObjectActive(state == State.Close);
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (state == State.Open)
			{
				SetState(State.Close);
			}
			else
			{
				SetState(State.Open);
			}
		}

		public override void PlayQuicklyClickSound()
		{
			if (base.itemData.quicklyClickSound != null && base.itemData.quicklyClickSound.Length == 2)
			{
				if (state == State.Close)
				{
					Singleton<AudioManager>.Instance.PlaySound(base.itemData.quicklyClickSound[0]);
				}
				else
				{
					Singleton<AudioManager>.Instance.PlaySound(base.itemData.quicklyClickSound[1]);
				}
			}
		}

		public override bool CanDestroyItem(BaseItem item)
		{
			if (state == State.Close)
			{
				return false;
			}
			if (item.transform.position.y >= destoryPoint.position.y)
			{
				return true;
			}
			return false;
		}

		public override Transform GetDestroyPoint()
		{
			if (destoryPoint != null)
			{
				return destoryPoint;
			}
			return base.GetDestroyPoint();
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
